<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="Block解读, Swift, iOS, Python">
    <meta name="description" content="群组欢迎加入群组,闲聊工作&amp;amp;技术&amp;amp;问题等

Block定义&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;看过&amp;lt;&amp;lt;Objective-C高级编程iOS与OS X多线">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Block解读 | waitwalker</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<!--导航-->

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    <!---->
                    <!--<img src="/medias/logo.png" class="logo-img" alt="LOGO">-->
                    <!---->
                    <span class="logo-span">waitwalker</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">waitwalker</div>
        <div class="logo-desc">
            
            Life is measured by thought and action not by time.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/waitwalker" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/waitwalker" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="https://github.com/waitwalker" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/13.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        Block解读
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Runtime/" target="_blank">
                                <span class="chip bg-color">Runtime</span>
                            </a>
                        
                            <a href="/tags/底层/" target="_blank">
                                <span class="chip bg-color">底层</span>
                            </a>
                        
                            <a href="/tags/源码/" target="_blank">
                                <span class="chip bg-color">源码</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Runtime源码解读/" class="post-category" target="_blank">
                                Runtime源码解读
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-04-18
                </div>

                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><strong>群组</strong><br><strong>欢迎加入群组,闲聊工作&amp;技术&amp;问题等</strong></p>
<p><img src="https://github.com/waitwalker/Resources/blob/master/Flutter/group/flutter_development_0923.JPG?raw=true" width="350" height="480" align="center"></p>
<h3 id="Block定义"><a href="#Block定义" class="headerlink" title="Block定义"></a>Block定义</h3><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;看过&lt;&lt;Objective-C高级编程iOS与OS X多线程和内存管理&gt;&gt;这本书的朋友应该知道其对block的定义:Blocks是C语言的扩充功能,带有自动变量(局部变量)的匿名函数.匿名函数就是不带有名称的函数.Block在不同的语言中有不同的命名.Python中叫Lambda,Swift中叫Closure(闭包)等.</p>
<p>我们知道根据作用域的不同变量大概有:自动变量(局部变量),静态变量(静态局部变量),静态全局变量,全局变量.而静态变量(局部和全局),全局变量其生命周期一般与程序同步,在程序的整个生命周期内,其也会保持在同一内存区域.</p>
<h3 id="block的声明结构"><a href="#block的声明结构" class="headerlink" title="block的声明结构"></a>block的声明结构</h3><pre><code>
// MARK: - blcok声明结构

struct Block_layout {

void *isa;

volatile int32_t flags; // contains ref count

int32_t reserved;

void (*invoke)(void *, ...);

struct Block_descriptor_1 *descriptor;

// imported variables

};

// MARK: - block 附件信息1

#define BLOCK_DESCRIPTOR_1 1

struct Block_descriptor_1 {

uintptr_t reserved;

uintptr_t size;

};

// MARK: - block 附件信息2

#define BLOCK_DESCRIPTOR_2 1

struct Block_descriptor_2 {

// requires BLOCK_HAS_COPY_DISPOSE

void (*copy)(void *dst, const void *src);

void (*dispose)(const void *);

};

// MARK: - block 附件信息3

#define BLOCK_DESCRIPTOR_3 1

struct Block_descriptor_3 {

// requires BLOCK_HAS_SIGNATURE

const char *signature;

const char *layout; // contents depend on BLOCK_HAS_EXTENDED_LAYOUT

};

struct Block_byref {

void *isa;

struct Block_byref *forwarding;

volatile int32_t flags; // contains ref count

uint32_t size;

};

struct Block_byref_2 {

// requires BLOCK_BYREF_HAS_COPY_DISPOSE

void (*byref_keep)(struct Block_byref *dst, struct Block_byref *src);

void (*byref_destroy)(struct Block_byref *);

};

struct Block_byref_3 {

// requires BLOCK_BYREF_LAYOUT_EXTENDED

const char *layout;

};

</code></pre><p>只在block_private.h中只找到block相关的声明结构,具体实现没有找到.其实现源码<a href="[http://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/BlocksRuntime/](http://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/BlocksRuntime/">这里</a>).下面我们看一下block_layout声明结构中的一些成员变量:</p>
<p>isa:block类型指针</p>
<p>flags:标志位,用于按bit操作</p>
<p>reserved:保留变量</p>
<p>invoke:指向block实现函数的指针</p>
<p>descriptor:附件信息</p>
<p>这些具体的结构我们可以通过clang编译器重写Objective-C文件转成.cpp文件查看源码.</p>
<h3 id="Block语法"><a href="#Block语法" class="headerlink" title="Block语法"></a>Block语法</h3><p>返回值类型 (^block名称)(形参参数列表) = ^返回值类型(参数列表){</p>
<p>return 返回值</p>
<p>}</p>
<p><img src="https://upload-images.jianshu.io/upload_images/1715253-5e36c4a5ffaa68f4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Block语法.jpg"></p>
<h3 id="Block截获自动变量"><a href="#Block截获自动变量" class="headerlink" title="Block截获自动变量"></a>Block截获自动变量</h3><p>带有自动变量的值在block中表现为截获自动变量值.</p>
<pre><code>
int valueOne = 18;

int valueTwo = 1;

// MARK: - 截获自动变量

void(^BlockOne)(void) = ^void(){

NSLog(@&quot;valueOne:%d; valueTwo:%d&quot;,valueOne,valueTwo);

};

// 修改自动变量的值

valueOne = 9;

// 调用block

BlockOne();

</code></pre><p>调用block后我们看到log输出:</p>
<pre><code>
2019-04-17 15:30:28.442314+0800 Block_Layout[6530:443419] valueOne:18; valueTwo:1

</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block内部捕获了valueOne和valueTwo两个自动变量,其中valueOne在block调用前进行了修改,而在block在真实调用时,valueOne值并没有跟随修改,因为block表达式截获得是自动变量的瞬间值,保存后就不能改写.所以在调用block时,即使改了block截获的自动变量的值,也不会影响block表达式内截获的值.</p>
<p>通过clang编译器重写一些文件,获取底层实现源码:</p>
<pre><code>
// block 声明结构

struct __block_impl {

void *isa;

int Flags;

int Reserved;

void *FuncPtr;

};

// block声明&amp;实现结构

void(*BlockOne)(void) = ((void (*)())&amp;__ViewController__viewDidLoad_block_impl_0((void *)__ViewController__viewDidLoad_block_func_0, &amp;__ViewController__viewDidLoad_block_desc_0_DATA, valueOne, valueTwo));

valueOne = 9;

// block调用

((void (*)(__block_impl *))((__block_impl *)BlockOne)-&gt;FuncPtr)((__block_impl *)BlockOne);

// MARK: - block具体实现结构

struct __ViewController__viewDidLoad_block_impl_0 {

struct __block_impl impl;

struct __ViewController__viewDidLoad_block_desc_0* Desc;

int valueOne;

int valueTwo;

// 同名的构造函数

__ViewController__viewDidLoad_block_impl_0(void *fp, struct __ViewController__viewDidLoad_block_desc_0 *desc, int _valueOne, int _valueTwo, int flags=0) : valueOne(_valueOne), valueTwo(_valueTwo) {

impl.isa = &amp;_NSConcreteStackBlock;

impl.Flags = flags;

impl.FuncPtr = fp;

Desc = desc;

}

};

// MARK: - block实现结构对应的实现函数

static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {

int valueOne = __cself-&gt;valueOne; // bound by copy

int valueTwo = __cself-&gt;valueTwo; // bound by copy

NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_pg_0qh5fq653wq1l3qz9_3ftg3r0000gn_T_ViewController_75dfcb_mi_0,valueOne,valueTwo);

}

// MARK: - desc结构

static struct __ViewController__viewDidLoad_block_desc_0 {

size_t reserved;

size_t Block_size;

} __ViewController__viewDidLoad_block_desc_0_DATA = { 0, sizeof(struct __ViewController__viewDidLoad_block_impl_0)};

</code></pre><p>具体看一下这几个结构:</p>
<p><strong>__block_impl:block对外暴露的声明结构,内部有四个成员变量.</strong></p>
<p><strong>_block_impl_0:block内部实现结构,其内部有一个同名的构造函数.</strong></p>
<p><strong>_block_func_0:block实现结构对应的实现函数,我们对block捕获的一些值都在这里操作,其参数为__cself指向的是Block自身.</strong></p>
<p><strong>_block_desc_0:desc结构,描述_block_impl_0的size等.</strong></p>
<p><strong>ViewController</strong>viewDidLoad_block_impl_0()构造函数,将fp函数指针赋值给impl的FuncPtr,将desc赋值给 Desc ,将flags赋值给impl的flags,将Block类型赋值给isa,之前我们已经讲过<a href="[https://waitwalker.github.io/2019/04/10/objc-object%E8%A7%A3%E8%AF%BB/](https://waitwalker.github.io/2019/04/10/objc-object%E8%A7%A3%E8%AF%BB/">isa</a>),可以具体查看,到此我们可以确定block的本质其实就是Objective-C对象,这些赋值后完成对impl进行初始化.</p>
<p><strong>blockOne就是一个_block_impl_0的实例.</strong></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可以以方法类比:__block_impl是对外的接口,其内部实现是_block_impl_0来实现的.我们可以看到block实现表达式中捕获的直接是valueOne,valueTwo的值.</p>
<p>如果我们尝试在block内部修改其捕获的自动变量的值,或马上报错:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/1715253-19d3e872cf58524f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="missing__block_error.png"></p>
<p>告诉我们缺少__block修饰符,下面我们在看一个情况:</p>
<pre><code>
NSMutableArray *array = [[NSMutableArray array]init];

// MARK: - 截获自动变量

void(^BlockOne)(void) = ^void(){

[array addObject:[NSObject new]];

};

</code></pre><p>这种情况可以正常编译过去,再看一下:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/1715253-8d9c0ade6fd90dcf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="missing__block.png"></p>
<p>同样的错误.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;然后我们用<strong>block修饰符修饰一下自动变量,编译正常通过.因此,如果我们需要修改block捕获的自动变量,需要在自动变量前面添加</strong>block修饰符.</p>
<h3 id="Block捕获静态-全局变量"><a href="#Block捕获静态-全局变量" class="headerlink" title="Block捕获静态/全局变量"></a>Block捕获静态/全局变量</h3><pre><code>
// 静态全局变量

static int valueTwo = 20;

// 全局变量

int valueThree = 30;

@interface ViewController ()

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *valueString;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 静态局部变量

static int valueOne = 10;

// MARK: - 截获静态变量,全局变量

void(^BlockOne)(void) = ^void(){

valueOne = 1;

valueTwo = 2;

valueThree = 3;

NSLog(@&quot;valueOne:%d,valueTwo:%d,valueThree:%d&quot;,valueOne,valueTwo,valueThree);

};

// 修改自动变量的值

valueOne = 9;

// 调用block

BlockOne();

}

@end

</code></pre><p>日志输出:</p>
<pre><code>
2019-04-17 16:56:57.112848+0800 MTTRuntime[7888:540053] valueOne:1,valueTwo:2,valueThree:3

</code></pre><p>然后我们继续用clang编译转换一下:</p>
<pre><code>
// block 声明和实现结构

void(*BlockOne)(void) = ((void (*)())&amp;__ViewController__viewDidLoad_block_impl_0((void *)__ViewController__viewDidLoad_block_func_0, &amp;__ViewController__viewDidLoad_block_desc_0_DATA, &amp;valueOne));

valueOne = 9;

// block调用

((void (*)(__block_impl *))((__block_impl *)BlockOne)-&gt;FuncPtr)((__block_impl *)BlockOne);

struct __ViewController__viewDidLoad_block_impl_0 {

struct __block_impl impl;

struct __ViewController__viewDidLoad_block_desc_0* Desc;

int *valueOne;

__ViewController__viewDidLoad_block_impl_0(void *fp, struct __ViewController__viewDidLoad_block_desc_0 *desc, int *_valueOne, int flags=0) : valueOne(_valueOne) {

impl.isa = &amp;_NSConcreteStackBlock;

impl.Flags = flags;

impl.FuncPtr = fp;

Desc = desc;

}

};

static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {

int *valueOne = __cself-&gt;valueOne; // bound by copy

// 静态局部变量

(*valueOne) = 1;

// 静态全局变量

valueTwo = 2;

// 全局变量

valueThree = 3;

NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_pg_0qh5fq653wq1l3qz9_3ftg3r0000gn_T_ViewController_9483ca_mi_0,(*valueOne),valueTwo,valueThree);

}

static struct __ViewController__viewDidLoad_block_desc_0 {

size_t reserved;

size_t Block_size;

} __ViewController__viewDidLoad_block_desc_0_DATA = { 0, sizeof(struct __ViewController__viewDidLoad_block_impl_0)};

</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我们可以看到静态全局变量和全局变量,被block捕获后直接修改的是其值,因为全局变量作用域于全局,在block内部进行修改后其值可以被保存下来.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;而静态局部变量访问的是使用指针对其进行访问,在block实现表达式里面传进去的也是&amp;valueOne地址.</p>
<p>block在捕获全局变量/静态变量时,可以在block实现表达式内部对其进行更改.block捕获静态局部变量的地址并保存,通过操作指针实现值的修改.</p>
<h3 id="Block捕获-block修饰的自动变量"><a href="#Block捕获-block修饰的自动变量" class="headerlink" title="Block捕获__block修饰的自动变量"></a>Block捕获__block修饰的自动变量</h3><pre><code>
- (void)viewDidLoad {

[super viewDidLoad];

// __block修饰的自动变量

__block int valueOne = 10;

// MARK: - 截获__block修饰的自动变量

void(^BlockOne)(void) = ^void(){

valueOne = 1;

NSLog(@&quot;valueOne:%d&quot;,valueOne);

};

// 修改自动变量的值

valueOne = 9;

// 调用block

BlockOne();

}

</code></pre><p>控制台输出:</p>
<pre><code>
2019-04-18 08:54:48.274042+0800 MTTRuntime[4012:43868] valueOne:1,valueTwo:20,valueThree:30

</code></pre><p>被__block修饰的自动变量,block捕获后能够对其进行更改.clang重写一下,查看源码:</p>
<pre><code>
// __block修饰的成员变量转换

__attribute__((__blocks__(byref))) __Block_byref_valueOne_0 valueOne = {(void*)0,(__Block_byref_valueOne_0 *)&amp;valueOne, 0, sizeof(__Block_byref_valueOne_0), 10};

// block的声明和实现表达式

void(*BlockOne)(void) = ((void (*)())&amp;__ViewController__viewDidLoad_block_impl_0((void *)__ViewController__viewDidLoad_block_func_0, &amp;__ViewController__viewDidLoad_block_desc_0_DATA, (__Block_byref_valueOne_0 *)&amp;valueOne, 570425344));

// 修改valueOne的值

(valueOne.__forwarding-&gt;valueOne) = 9;

//block调用

((void (*)(__block_impl *))((__block_impl *)BlockOne)-&gt;FuncPtr)((__block_impl *)BlockOne);

// byref结构

struct __Block_byref_valueOne_0 {

void *__isa;

__Block_byref_valueOne_0 *__forwarding;//指向自己的指针

int __flags;

int __size;

int valueOne;

};

//block匿名实现结构

struct __ViewController__viewDidLoad_block_impl_0 {

struct __block_impl impl;

struct __ViewController__viewDidLoad_block_desc_0* Desc;

__Block_byref_valueOne_0 *valueOne; // by ref

__ViewController__viewDidLoad_block_impl_0(void *fp, struct __ViewController__viewDidLoad_block_desc_0 *desc, __Block_byref_valueOne_0 *_valueOne, int flags=0) : valueOne(_valueOne-&gt;__forwarding) {

impl.isa = &amp;_NSConcreteStackBlock;

impl.Flags = flags;

impl.FuncPtr = fp;

Desc = desc;

}

};

// block实现函数

static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {

__Block_byref_valueOne_0 *valueOne = __cself-&gt;valueOne; // bound by ref

(valueOne-&gt;__forwarding-&gt;valueOne) = 1;

NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_pg_0qh5fq653wq1l3qz9_3ftg3r0000gn_T_ViewController_ea5a30_mi_0,(valueOne-&gt;__forwarding-&gt;valueOne));

}

// block copy

static void __ViewController__viewDidLoad_block_copy_0(struct __ViewController__viewDidLoad_block_impl_0*dst, struct __ViewController__viewDidLoad_block_impl_0*src) {_Block_object_assign((void*)&amp;dst-&gt;valueOne, (void*)src-&gt;valueOne, 8/*BLOCK_FIELD_IS_BYREF*/);}

// block dispose

static void __ViewController__viewDidLoad_block_dispose_0(struct __ViewController__viewDidLoad_block_impl_0*src) {_Block_object_dispose((void*)src-&gt;valueOne, 8/*BLOCK_FIELD_IS_BYREF*/);}

</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可以看到,这里多了<strong>Block_byref_valueOne_0结构,我们初始化的自动变量也被转换成了这个结构的实例,自动变量初始化值为10,这个值也出现在</strong>Block_byref_valueOne_0结构的初始化中,并且这个结构持有这个变量,使其成为成员变量.</p>
<pre><code>
// byref结构

struct __Block_byref_valueOne_0 {

void *__isa;

__Block_byref_valueOne_0 *__forwarding;//指向自己的指针

int __flags;

int __size;

int valueOne;

};

</code></pre><p>我们再来看一下怎么访问这个自动变量的:</p>
<pre><code>
// block实现函数

static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {

__Block_byref_valueOne_0 *valueOne = __cself-&gt;valueOne; // bound by ref

(valueOne-&gt;__forwarding-&gt;valueOne) = 1;

NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_pg_0qh5fq653wq1l3qz9_3ftg3r0000gn_T_ViewController_ea5a30_mi_0,(valueOne-&gt;__forwarding-&gt;valueOne));

}

</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;是通过((<strong>Block_byref_valueOne_0 *)valueOne-&gt;</strong>forwarding-&gt;valueOne).Block的_block_impl_0结构实例指针持有指向<strong>Block_byref_valueOne_0的指针;而</strong>Block_byref_valueOne_0的成员变量持有指向自身的指针.然后通过<strong>forwarding来访问valueOne.</strong>Block_byref_valueOne_0结构并不在_block_impl_0中,这样做主要是在多个Block中使用__block.</p>
<h3 id="Block类型"><a href="#Block类型" class="headerlink" title="Block类型"></a>Block类型</h3><p>_NSConcreteGlobalBlock:定义在全局区,存储于程序数据区</p>
<p>_NSConcreteStackBlock:存储于栈区,超出作用域被回收</p>
<p>_NSConcreteMallocBlock:存储于堆区,从栈copy过来</p>
<h4 id="Block复制到堆上的情况"><a href="#Block复制到堆上的情况" class="headerlink" title="Block复制到堆上的情况:"></a>Block复制到堆上的情况:</h4><p>1)block调用了copy方法;<br></p>
<p>2)block作为方法返回值;<br></p>
<p>3)将block赋值给__strong修饰的id类型或赋值给类的block成员变量;<br></p>
<p>4)方法名中有usingBlock的cocoa框架或者GCD相关api;<br></p>
<h3 id="Block循环引用"><a href="#Block循环引用" class="headerlink" title="Block循环引用"></a>Block循环引用</h3><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果Block中使用附有<strong>strong修饰符的对象类型自动变量,那么Block从栈复制到堆上时,该对象为Block所持有,这样会引起循环引用问题,可以通过</strong>weak修饰符来打破循环引用,这个我们在后面细讲__weak实现原理.</p>
<h3 id="编译后源码库"><a href="#编译后源码库" class="headerlink" title="编译后源码库"></a>编译后源码库</h3><p><strong>编译后的源码放在<a href="https://github.com/waitwalker/Runtime" target="_blank" rel="noopener">Github</a>, 如果对你有帮助,请给一个star吧!</strong></p>
<h3 id="博客地址-amp-相关文章"><a href="#博客地址-amp-相关文章" class="headerlink" title="博客地址&amp;相关文章"></a>博客地址&amp;相关文章</h3><p><strong>博客地址:</strong> <a href="https://waitwalker.github.io/">https://waitwalker.github.io/</a></p>
<p>系列文章:</p>
<p><a href="https://waitwalker.github.io/2019/04/08/Runtime%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91/">1. Runtime源码编译</a></p>
<p><a href="https://waitwalker.github.io/2019/04/10/objc-object%E8%A7%A3%E8%AF%BB/">2. objc_object解读</a></p>
<p><a href="https://waitwalker.github.io/2019/04/12/Method%E8%A7%A3%E8%AF%BB/">3. Method解读</a></p>
<p><a href="https://waitwalker.github.io/2019/04/15/Class%E8%A7%A3%E8%AF%BB/">4. Class解读</a></p>
<p><a href="https://waitwalker.github.io/2019/04/15/Ivar-objc-property-t-Protocol%E8%A7%A3%E8%AF%BB/">5. Ivar objc_property_t Protocol解读</a></p>
<p><a href="https://waitwalker.github.io/2019/04/18/Block%E8%A7%A3%E8%AF%BB/">6. Block解读</a></p>
<p><a href="https://waitwalker.cn/2019/04/22/Retain-Release解读/" target="_blank" rel="noopener">7. Retain&amp;Release解读</a></p>
<p><a href="http://waitwalker.cn/2019/04/23/Autorelease解读/" target="_blank" rel="noopener">8. Autorelease解读</a></p>
<p><a href="http://waitwalker.cn/2019/04/24/Weak解读/" target="_blank" rel="noopener">9. Weak解读</a></p>
<p><a href="http://waitwalker.cn/2019/04/25/msgSend-解读/" target="_blank" rel="noopener">10. msgSend()解读</a></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">
                        <i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;转载请注明:
                    </span>
                    <a href="https://waitwalker.github.io" class="b-link-green">waitwalker</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2019/04/18/Block解读/" class="b-link-green">Block解读</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/04/22/Retain-Release解读/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="Retain&Release解读">
                        
                        <span class="card-title">Retain&Release解读</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">群组欢迎加入群组,闲聊工作&amp;技术&amp;问题等

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iOS通过引用计数(Reference Counting)机制来管理内存,当一个对象</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-04-22
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Runtime源码解读/" class="post-category" target="_blank">
                                    Runtime源码解读
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Runtime/" target="_blank">
                        <span class="chip bg-color">Runtime</span>
                    </a>
                    
                    <a href="/tags/底层/" target="_blank">
                        <span class="chip bg-color">底层</span>
                    </a>
                    
                    <a href="/tags/源码/" target="_blank">
                        <span class="chip bg-color">源码</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/04/16/二叉树/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/20.jpg" class="responsive-img" alt="二叉树">
                        
                        <span class="card-title">二叉树</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">群组欢迎加入群组,闲聊工作&amp;技术&amp;问题等

二叉树的定义&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;二叉树(Binary Tree)是一棵树,其中每个节点都不能有多余两个的</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-04-16
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/数据结构与算法/" class="post-category" target="_blank">
                                    数据结构与算法
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/数据结构/" target="_blank">
                        <span class="chip bg-color">数据结构</span>
                    </a>
                    
                    <a href="/tags/算法/" target="_blank">
                        <span class="chip bg-color">算法</span>
                    </a>
                    
                    <a href="/tags/Swift/" target="_blank">
                        <span class="chip bg-color">Swift</span>
                    </a>
                    
                    <a href="/tags/二叉树/" target="_blank">
                        <span class="chip bg-color">二叉树</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: waitwalker<br />'
            + '作者: waitwalker<br />'
            + '链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站在原作者&copy;<a href="https://blinkfox.github.io/" target="_blank">Blinkfox</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题基础上搭建.

            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis"><!---->
<a href="https://github.com/waitwalker" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="https://twitter.com/284485487" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-twitter"></i>
</a>

<a href="https://www.facebook.com/profile.php?id=100003684945279" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-facebook"></i>
</a>

<a href="https://t.me/joinchat/Ej0o0A1ntlq5ZFIMzzO5Pw" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-telegram"></i>
</a>

<a href="https://www.weibo.com/1789419613/profile?topnav=1&wvr=6&is_all=1" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-weibo"></i>
</a>


<!--原来-->
<!---->
    <!--<a href="mailto:waitwalker@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-envelope-open"></i>-->
    <!--</a>-->
<!---->

<!---->
    <!--<a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=284485487" class="tooltipped" data-tooltip="QQ联系我: 284485487" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-qq"></i>-->
    <!--</a>-->
<!---->

<!---->
    <!--<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-rss"></i>-->
    <!--</a>-->
<!----></div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->




    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>